package cn.regexp.business.web.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.regexp.common.base.page.TablePageInfo;
import cn.regexp.common.web.AjaxResult;
import cn.regexp.domain.ShopCanteen;
import cn.regexp.query.CanteenQuery;
import cn.regexp.service.ICanteenService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;

/**
 * 餐厅控制器
 */
@Controller
@RequestMapping("business/canteens")
public class CanteenController {
    //模板前缀
    private static final String prefix = "business/canteens/";

    @Autowired
    private ICanteenService canteensService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:canteens:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("business:canteens:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }

    @RequiresPermissions("business:canteens:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("canteen", canteensService.getById(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:canteens:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ShopCanteen> query(CanteenQuery qo){
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<ShopCanteen> wrapper = new QueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(qo.getKeyword()), "canteen_name", qo.getKeyword())
                .or()
                .like(ObjectUtil.isNotEmpty(qo.getKeyword()), "address", qo.getKeyword());
        return new TablePageInfo<>(canteensService.list(wrapper), canteensService.count(wrapper));
    }


    //新增
    @RequiresPermissions("business:canteens:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ShopCanteen canteen){
        canteensService.save(canteen);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:canteens:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ShopCanteen canteen){
        canteensService.updateById(canteen);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:canteens:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        canteensService.removeByIds(Collections.singletonList(ids));
        return AjaxResult.success();
    }
}
